/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.treepoke.util.mail;

import com.treepoke.util.dto.MailDTO;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.NamingException;

public class Mailer {

    private Properties propSession;

    public String getHost() {
        if (propSession != null && propSession.containsKey("mail.smtp.host")) {
            return propSession.getProperty("mail.smtp.host");
        } else {
            return null;
        }
    }

    public String getPassword() {
        if (propSession != null && propSession.containsKey("mail.smtp.password")) {
            return propSession.getProperty("mail.smtp.password");
        } else {
            return null;
        }
    }

    public String getPort() {
        if (propSession != null && propSession.containsKey("mail.smtp.port")) {
            return propSession.getProperty("mail.smtp.port");
        } else {
            return null;
        }
    }

    public Properties getProperties() {
        return propSession;
    }

    public String getUser() {
        if (propSession != null && propSession.containsKey("mail.smtp.user")) {
            return propSession.getProperty("mail.smtp.user");
        } else {
            return null;
        }
    }

    public void setHost(String host) {
        if (propSession == null) {
            propSession = new Properties();
        }
        propSession.setProperty("mail.smtp.host", host);
    }

    public void setPassword(String password) {
        if (propSession == null) {
            propSession = new Properties();
        }
        propSession.setProperty("mail.smtp.password", password);
    }

    public void setPort(String port) {
        if (propSession == null) {
            propSession = new Properties();
        }
        propSession.setProperty("mail.smtp.port", port);
    }

    public void setUser(String user) {
        if (propSession == null) {
            propSession = new Properties();
        }
        propSession.setProperty("mail.smtp.user", user);
    }

    public void setProperties(Properties propSession) {
        this.propSession = propSession;
    }

    private class Authenticator extends javax.mail.Authenticator {

        private PasswordAuthentication authentication;

        public Authenticator(String username, String password) {
            authentication = new PasswordAuthentication(username, password);
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return authentication;
        }
    }

    private Session getSession() throws NamingException {
        Authenticator authenticator = new Authenticator(propSession.getProperty("mail.smtp.user"), propSession.getProperty("mail.smtp.password"));
        propSession.setProperty("mail.smtp.auth", "true");
        propSession.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
        return Session.getInstance(propSession, authenticator);
    }

    public void sendMail(MailDTO msg) throws NamingException, MessagingException {

        Session session = getSession();
        // create a message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(msg.getFrom()));
        String[] to = msg.getTo().split(";");
        InternetAddress[] toAddress = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            toAddress[i] = new InternetAddress(to[i]);
        }
        message.setRecipients(Message.RecipientType.TO, toAddress);
        
        if (msg.getCc() != null) {
            String[] cc = msg.getCc().split(";");
            InternetAddress[] ccAddress = new InternetAddress[cc.length];
            for (int i = 0; i < cc.length; i++) {
                ccAddress[i] = new InternetAddress(cc[i]);
            }
            message.setRecipients(Message.RecipientType.CC, ccAddress);
        }

        if (msg.getBcc() != null) {
            String[] bcc = msg.getBcc().split(";");
            InternetAddress[] bccAddress = new InternetAddress[bcc.length];
            for (int i = 0; i < bcc.length; i++) {
                bccAddress[i] = new InternetAddress(bcc[i]);
            }
            message.setRecipients(Message.RecipientType.BCC, bccAddress);
        }
        
        message.setSubject(msg.getSubject());

        // create and fill the first message part                                            
        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        MimeBodyPart mbpContent = new MimeBodyPart();
        mbpContent.setText(msg.getContent());
        mp.addBodyPart(mbpContent);
        // Adjunta todos los archivos al correo
        for (File file : msg.getAttach()) {
            // create the second message part
            MimeBodyPart mbpFile = new MimeBodyPart();
            // attach the file to the message
            FileDataSource fds = new FileDataSource(file.getAbsoluteFile());
            mbpFile.setDataHandler(new DataHandler(fds));
            mbpFile.setFileName(fds.getName());
            mp.addBodyPart(mbpFile);
        }

        // add the Multipart to the message            
        message.setContent(mp);
        // set the Date: header
        message.setSentDate(new Date(System.currentTimeMillis()));
        // send the message
        Transport.send(message);

    }
}